//
//  CSLaunchViewController.m
//  ChinaShip
//
//  Created by Yale on 2019/1/28.
//  Copyright © 2019年 ChinaShip. All rights reserved.
//

#import "CSLaunchViewController.h"
@interface CSLaunchViewController (){
    NSInteger totalSecond;
}
@property (nonatomic,strong)UIControl   *timeBgControl;
@property (nonatomic,strong)UILabel     *timeLabel;
@property (nonatomic,strong)UIImageView *showImageView;
@property (nonatomic,strong)NSTimer     *countTimer;
@end

@implementation CSLaunchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    totalSecond = 6;
    // Do any additional setup after loading the view.
    [self.view addSubview:self.showImageView];
    [self.timeBgControl addSubview:self.timeLabel];
    [self.view addSubview:self.timeBgControl];
    [self startTimer];
}

- (UIImageView *)showImageView{
    if (!_showImageView) {
        _showImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, kSCREEN_HEIGHT)];
        _showImageView.contentMode = UIViewContentModeScaleAspectFill;
        _showImageView.clipsToBounds = YES;
        _showImageView.image = [UIImage imageNamed:@"lunch_1"];
    }
    return _showImageView;
}

- (UIControl *)timeBgControl{
    if (!_timeBgControl) {
        CGRect cFrame = CGRectZero;
        if (isIPhonex) {
            cFrame = CGRectMake(kSCREEN_WIDTH-110, 50, 90, 32);
        }else{
            cFrame = CGRectMake(kSCREEN_WIDTH-110, 25, 90, 32);
        }
        _timeBgControl = [[UIControl alloc] initWithFrame:cFrame];
        _timeBgControl.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.4];
        _timeBgControl.layer.cornerRadius = 16.0f;
        [_timeBgControl addTarget:self action:@selector(enterHomeAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _timeBgControl;
}

- (UILabel *)timeLabel{
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc]initWithFrame:self.timeBgControl.bounds];
        _timeLabel.font = [UIFont systemFontOfSize:14.0f];
        _timeLabel.textColor = [UIColor whiteColor];
        _timeLabel.textAlignment = NSTextAlignmentCenter;
        _timeLabel.text = [NSString stringWithFormat:@"SKIP%lds",totalSecond];
    }
    return _timeLabel;
}

//进入主页
- (void)enterHomeAction{
    [self.countTimer invalidate];
    self.countTimer = nil;
    [[UIApplication sharedApplication].keyWindow setRootViewController:[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateInitialViewController]];
}


- (void)startTimer{
    self.countTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countTimerHandler) userInfo:nil repeats:YES];
    [self.countTimer fire];
}

- (void)countTimerHandler{
    if (totalSecond == 0) {
        [self enterHomeAction];
    }else{
        _timeLabel.text = [NSString stringWithFormat:@"SKIP%lds",totalSecond];
        totalSecond --;
        if (totalSecond == 2) {
            
            self.showImageView.image = [UIImage imageNamed:@"lunch_2"];
            CATransition *transition = [CATransition animation];
            transition.duration = 0.3f;
            transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
            transition.type = kCATransitionFade;
            [self.showImageView.layer addAnimation:transition forKey:nil];
            
        }
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
